<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用入口文件

// 检测PHP环境
if(version_compare(PHP_VERSION,'5.3.0','<'))  die('require PHP > 5.3.0 !');

// 程序目录 TODO delete
define('MITU_ROOT', dirname(dirname(__FILE__)) . '/');
define('WEB_ROOT', MITU_ROOT . "Public/");
define('URL_1','/mall/Public/index.php');
// 环境检测
$env = detectWebEnv(array(
    'dev' => '*local*',
    'testing' => 'tmall.haascloud.cn',
    'production' => 'mall.haascloud.cn',
));

define('APP_STATUS', $env);

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
// 调试模式 建议开发阶段开启 部署阶段注释或者设为false
if ('production' === APP_STATUS) {
    define('APP_DEBUG', false);
} else {
    define('APP_DEBUG', true);
}
//define('APP_DEBUG', true);
// 定义应用目录
define('APP_PATH','../Application/');


// 引入ThinkPHP入口文件
require '../ThinkPHP/ThinkPHP.php';

// 亲^_^ 后面不需要任何代码了 就是如此简单


/**
 * 一个检测环境的方法
 * production, testing, dev
 * @param $environments
 * @return int|string
 */
function detectWebEnv($environments)
{
//    if (!isset($_SERVER['SERVER_NAME'])) {
//        //命令执行时获取不到域名时才会执行该方法
//        $devMachines = array('Peters-Mbp-5.local');//自定义
//        $testMachines = array('miot-232');//自定义
//        if (in_array(gethostname(), $testMachines)) {
//            return 'test';
//        } else if (in_array(gethostname(), $devMachines)) {
//            return 'dev';
//        } else {
//            return 'production';
//        }
//    }

    foreach ($environments as $environment => $hosts) {
        foreach ((array)$hosts as $host) {
            if (str_is($host, $_SERVER['SERVER_NAME'])) {
                return $environment;
            }
        }
    }

    return 'production';
}

/**
 * 匹配
 * @param $pattern
 * @param $value
 * @return bool
 */
function str_is($pattern, $value)
{
    if ($pattern == $value) return true;

    $pattern = preg_quote($pattern, '#');

    $pattern = str_replace('\*', '.*', $pattern) . '\z';

    return (bool)preg_match('#^' . $pattern . '#', $value);
}

/**************functions to forbid sql injection****************/
// add slashe for a string
function m_addslashes($string, $force = 0, $strip = FALSE)
{
    if ($force) {
        if (is_array($string)) {
            foreach ($string as $key => $val) {
                $string[$key] = m_addslashes($val, $force, $strip);
            }
        } else {
            $string = eregi_replace("union", '', $string);
            $string = eregi_replace("having", '', $string);
            $string = addslashes($strip ? stripslashes($string) : $string);
        }
    }
    return $string;
}

// parse value 2 int
function m_parse_int($val)
{
    return intval($val);
}

// parse value 2 float
function m_parse_float($val)
{
    return floatval($val);
}

// parse a request parameter
function m_parse_param($val)
{
    return m_addslashes($val, 1, true);
}